package xml;

import javax.xml.bind.annotation.adapters.*;
import cuShapes.*;
import java.util.*;

public class ShapeCollectionAdapter extends XmlAdapter<ShapeListType, List<PQShape>> {
	// adapt original Java construct to a type, NotificationsType,
	// which we can easily map to the XML output we want
	public ShapeListType marshal(List<PQShape> shapes) throws Exception {
		List<Box> boxes = new ArrayList<Box>();
		List<Ellipse> ellipses = new ArrayList<Ellipse>();

		for (PQShape e : shapes) {
			if (e instanceof Box){
				boxes.add((Box) e);
			} else if (e instanceof Ellipse) {
				ellipses.add((Ellipse) e);
			}
		}
		return new ShapeListType(boxes, ellipses);
	}

	// reverse operation: map XML type to Java
	public List<PQShape> unmarshal(ShapeListType shapeList) throws Exception { 
		List<PQShape> shapes = new ArrayList<PQShape>();
        shapes.addAll(shapeList.getBoxes());
        shapes.addAll(shapeList.getEllipses());
        
        return shapes;
	}
}
